package com.lemon.funplus;

import java.util.ArrayList;

import yuku.ambilwarna.AmbilWarnaDialog;
import yuku.ambilwarna.AmbilWarnaDialog.OnAmbilWarnaListener;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.graphics.drawable.shapes.PathShape;
import android.graphics.drawable.shapes.Shape;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.GestureDetector;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.lemon.funplus.cheanh.BrushSettingDialog;
import com.lemon.funplus.cheanh.CanvasGestureListener;
import com.lemon.funplus.cheanh.CanvasTouchListener;
import com.lemon.funplus.cheanh.DrawingView;
import com.lemon.funplus.cheanh.EraserSettingDialog;
import com.lemon.funplus.cheanh.TrollSelectDialog;
import com.lemon.funplus.ulti.BaseEngine;

@SuppressLint("NewApi")
public class CheAnhActivity extends BaseActivity {
	Bitmap bitmap;
	RelativeLayout layout;
	public DrawingView myView;
	protected OnTouchListener touchListener;
	protected GestureDetector gdt;

	public ArrayList<Drawable> visibleDrawables = new ArrayList<Drawable>();
	protected Shape currentShape = new OvalShape();
	public ShapeDrawable currentDrawable = new ShapeDrawable(currentShape);
	public Path currentPath;

	public static int defaultColor = Color.RED;
	public static int defaultRadius = 10;
	public static int defaultBackground = Color.WHITE;

	public static enum ShapeType {
		BRUSH, ERASER, OVAL, RECTANGLE
	}

	public int color = defaultColor;
	public int brushRadius = defaultRadius;
	public int eraseRadius = defaultRadius;
	public int alpha = 255;
	public int backgroundColor = defaultBackground;
	public ShapeType currShape = ShapeType.BRUSH;

	public String filePath;

	public LinearLayout layout_preview;

	public boolean popupVisible = false;

	public String pathTroll = "cuoi";
	String[] imagelist;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.cheanh_layout);
		initView();

		myView = new DrawingView(this, CheAnhActivity.this);
		myView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

		layout.addView(myView);
		gdt = new GestureDetector(this, new CanvasGestureListener(CheAnhActivity.this));
		touchListener = new CanvasTouchListener(CheAnhActivity.this, gdt);
		myView.setOnTouchListener(touchListener);
		previewImagetroll();

	}

	private void initView() {
		layout = (RelativeLayout) findViewById(R.id.frame_layout);
		layout_preview = (LinearLayout) findViewById(R.id.cheanh_linearlayout_Preview);
		findViewById(R.id.menu_photo).setOnClickListener(clickListener);
		findViewById(R.id.menu_brush).setOnClickListener(clickListener);
		findViewById(R.id.menu_color).setOnClickListener(clickListener);
		findViewById(R.id.menu_clear).setOnClickListener(clickListener);
		findViewById(R.id.menu_save).setOnClickListener(clickListener);
		findViewById(R.id.menu_text).setOnClickListener(clickListener);
		findViewById(R.id.menu_troll).setOnClickListener(clickListener);
		findViewById(R.id.menu_eraser).setOnClickListener(clickListener);
	}

	OnClickListener clickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.menu_photo:
				chooseImage();
				break;
			case R.id.menu_troll:
				selectTroll();
				break;
			case R.id.menu_color:
				showColorPicker();
				break;
			case R.id.menu_clear:
				clear();
				break;
			case R.id.menu_eraser:
				currShape = ShapeType.ERASER;
				EraserSettingDialog settingDialog = EraserSettingDialog.newInstance(CheAnhActivity.this);
				settingDialog.show(getSupportFragmentManager(), null);
				break;
			case R.id.menu_brush:
				currShape = ShapeType.BRUSH;
				BrushSettingDialog brushSettingDialog = BrushSettingDialog.newInstance(CheAnhActivity.this);
				brushSettingDialog.show(getSupportFragmentManager(), null);
				break;
			default:
				break;
			}
		}
	};

	public void selectTroll() {
		TrollSelectDialog dialog=TrollSelectDialog.newInstance(CheAnhActivity.this);
		dialog.show(getSupportFragmentManager(), null);
	}

	public void showColorPicker() {
		AmbilWarnaDialog dialog = new AmbilWarnaDialog(this, CheAnhActivity.this.color, new OnAmbilWarnaListener() {
			@Override
			public void onOk(AmbilWarnaDialog dialog, int color) {
				CheAnhActivity.this.color = color;
			}

			@Override
			public void onCancel(AmbilWarnaDialog dialog) {

			}
		});
		dialog.show();
	}

	public void chooseImage() {
		Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
		startActivityForResult(i, 0);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case 0:
			if (requestCode == 0 && resultCode == RESULT_OK && null != data) {
				bitmap = null;
				Uri selectedImage = data.getData();
				String[] filePathColumn = { MediaStore.Images.Media.DATA };

				Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
				cursor.moveToFirst();

				int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
				String picturePath = cursor.getString(columnIndex);
				cursor.close();
//				System.err.println("Image Path =====>" + picturePath);
				bitmap = BitmapFactory.decodeFile(picturePath);
				// getImageLayout(bitmap);
			}
			break;
		}
	}

	public void previewImagetroll() {
		layout_preview.removeAllViews();
		imagelist = BaseEngine.getNamefileAsset(CheAnhActivity.this, pathTroll);
		for (int i = 0; i < imagelist.length; i++) {
			ImageView imageView = new ImageView(CheAnhActivity.this);
			LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
			layoutParams.setMargins(5, 5, 5, 5);
			imageView.setAdjustViewBounds(true);
			imageView.setScaleType(ScaleType.CENTER_INSIDE);
			imageView.setImageBitmap(BaseEngine.getBitmapFromAsset(CheAnhActivity.this, pathTroll + "/" + imagelist[i]));
			layout_preview.addView(imageView, layoutParams);
		}
	}

	public void setCurrentShape() {
		switch (currShape) {
		case BRUSH:
			setPath(false);
			break;
		case ERASER:
			setPath(true);
			break;
		case OVAL:
			currentShape = new OvalShape();
			currentDrawable = new ShapeDrawable(currentShape);
			break;
		}
	}

	public void clear() {
		visibleDrawables.clear();
		myView.invalidate();
	}

	private void setPath(boolean erase) {
		currentPath = new Path();
		currentShape = new PathShape(currentPath, myView.canvasWidth, myView.canvasHeight);
		currentDrawable = new ShapeDrawable(currentShape);
		currentDrawable.setBounds(0, 0, myView.canvasWidth, myView.canvasHeight);

		Paint curr = currentDrawable.getPaint();
		curr.setColor(color);
		curr.setAlpha(alpha);
		curr.setStyle(Paint.Style.STROKE);
		curr.setStrokeWidth(brushRadius);
		curr.setStrokeJoin(Paint.Join.ROUND);
		curr.setStrokeCap(Paint.Cap.ROUND);
		curr.setAntiAlias(true);
		curr.setDither(true);

		if (erase == true) {
			curr.setStrokeWidth(eraseRadius);
			curr.setColor(backgroundColor);
		}
	}

}
